import { server } from '../config/server';
import { useToken } from '../utils/token';

export const book = {
    /*
     * 获取推荐参考书
     */
    getRecommendBook(lastId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/book/getRecommendBook`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: { lastId },
                success: (res) => {
                    console.log('【 Response in request getRecommendBook 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request getRecommendBook 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    /*
     * 加入书架
     */
    joinInLib(bookId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/book/joinInLib`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: { bookId },
                success: (res) => {
                    console.log('【 Response in request joinInLib 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request joinInLib 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    /*
     * 取消加入书架
     */
    noJoinInLib(bookId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/book/noJoinInLib`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: { bookId },
                success: (res) => {
                    console.log('【 Response in request noJoinInLib 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request noJoinInLib 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    /*
     * 取消加入书架
     */
    getJoinBook(lastId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/book/getJoinBook`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: { lastId },
                success: (res) => {
                    console.log('【 Response in request getJoinBook 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request getJoinBook 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    /*
     * 获取书本内容
     */
    getBookContent(bookId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/book/getBookContent`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: { bookId },
                success: (res) => {
                    console.log('【 Response in request getBookContent 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request getBookContent 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    // 搜索书本
    searchBook(bookName) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/book/searchBook`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: { bookName },
                success: (res) => {
                    console.log('【 Response in request searchBook 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request searchBook 】', err);
                    reject({ success: false, err });
                }
            });
        });
    }
};